package github.fafaovo.common.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.Hand;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.server.permission.PermissionAPI;
import net.minecraftforge.server.permission.context.PlayerContext;


public class GetHandCommand {
    // [then是空格] 即命令是/example getHand
    public GetHandCommand(CommandDispatcher<CommandSource> dispatcher)
    {
        dispatcher.register(Commands.literal("example").then(Commands.literal("getHand").executes(
                (c)->{
                    return getHand(c.getSource());
                }
        )));
    }
    private int getHand(CommandSource source) throws CommandSyntaxException
    {


        ServerPlayerEntity player = source.asPlayer();
        //有管理员权限才能运行的指令
        String permissionKey = "a.random.permission.key";
        if (PermissionAPI.hasPermission(player.getGameProfile(), permissionKey, new PlayerContext(player))) {
            // 在有权限的时候执行操作
            ItemStack heldItem = player.getHeldItem(Hand.MAIN_HAND);
            if(heldItem.getItem().equals(Items.AIR))
            {
                return 1;
            }
            player.addItemStackToInventory(new ItemStack(heldItem.getItem(),heldItem.getCount()));

            source.sendFeedback(new TranslationTextComponent("get "+ heldItem.getItem() ),true);
            return 1;
        }
        return 1;


    }
}
